package com.cj.park_dispatch.utils

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.util.Base64
import android.view.Gravity
import android.view.View
import android.view.animation.TranslateAnimation
import android.widget.ImageView
import android.widget.Toast
import androidx.fragment.app.Fragment
import coil.load
import coil.transform.RoundedCornersTransformation
import com.cj.park_dispatch.R
import com.cj.park_dispatch.http.BaseObserver
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import java.math.BigDecimal
import java.util.concurrent.TimeUnit


/**
 * ━━━━━━神兽出没━━━━━━
 * 　　　┏┓　　　┏┓
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　　　　　┃
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　　　　　┃
 * 　　┃　　　┻　　　┃
 * 　　┃　　　　　　　┃
 * 　　┗━┓　　　┏━┛Code is far away from bug with the animal protecting
 * 　　　　┃　　　┃    神兽保佑,代码无bug
 * 　　　　┃　　　┃
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ━━━━━━感觉萌萌哒━━━━━━
 *
 * Created by yi on 2020/9/3.
 */
inline fun <reified T : Activity> Context.clickNextPage(view: View) {
    view.setOnClickListener {
        gotoAct<T>()
    }
}

inline fun <reified T : Activity> Context.gotoAct() {
    val intent = Intent(this, T::class.java)
    startActivity(intent)
}

fun Context.gotoAct(intent: Intent) {
    startActivity(intent)
}

fun Context.toast(string: String) {
    Toast.makeText(this, string, Toast.LENGTH_SHORT).show()
}

fun Activity.toast(string: String) {
    toastStr(context = this, str = string)
}

fun Fragment.toast(string: String) {
    activity?.let { toastStr(context = it, str = string) }
}

fun ImageView.showImg10(url: String) {
    load(url) {
        placeholder(R.mipmap.ic_launcher)
        error(R.mipmap.ic_launcher)
        crossfade(true)
        transformations(RoundedCornersTransformation(10f))
    }
}

fun ImageView.showImg(url: String) {
    load(url) {
        placeholder(R.mipmap.ic_launcher)
        error(R.mipmap.ic_launcher)
        crossfade(true)
    }
}


/**
 * 统一显示信息
 */
private var mToast: Toast? = null

fun toastStr(context: Context, str: String?) {
    if (mToast == null) {
        mToast = Toast.makeText(context, str, Toast.LENGTH_SHORT)
    } else {
        mToast!!.setText(str)
    }
    mToast!!.setGravity(Gravity.CENTER, 0, 0)
    mToast!!.show()
}

fun getTranslateAnim(duration: Long, fromY: Float, toY: Float): TranslateAnimation {
    val translateAnimation = TranslateAnimation(
        TranslateAnimation.RELATIVE_TO_SELF,
        0f,
        TranslateAnimation.RELATIVE_TO_SELF,
        0f,
        TranslateAnimation.RELATIVE_TO_SELF,
        fromY,
        TranslateAnimation.RELATIVE_TO_SELF,
        toY
    )
    translateAnimation.duration = duration
    return translateAnimation
}

fun <T> execute(observable: Observable<T>, observer: BaseObserver<T>) {
    observable.subscribeOn(Schedulers.io())
        .unsubscribeOn(Schedulers.io())
        .observeOn(
            AndroidSchedulers.mainThread(),
            true
        ) //.compose(RxUtils.bindToLifecycle(activity))
        .subscribe(observer)
}

fun stringToBitmap(string: String): Bitmap? {
    var bitmap: Bitmap? = null
    try {
        val bitmapArray: ByteArray =
            Base64.decode(string.split(",".toRegex()).toTypedArray()[1], Base64.DEFAULT)
        bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.size)
    } catch (e: Exception) {
        e.printStackTrace()
    }
    return bitmap
}

fun interval(initialDelay: Long, period: Long, unit: TimeUnit, observer: LongObserver) {
    Observable.interval(initialDelay, period, unit)
        .subscribe(observer)
}

fun intervalRange(start: Long, count: Long, initialDelay: Long, period: Long, unit: TimeUnit, observer: LongObserver) {
    Observable.intervalRange(start, count, initialDelay, period, unit)
        .subscribe(observer)

}

fun division(v1: Double, v2: Double, scale: Int): Double {
    require(scale >= 0) { "The scale must be a positive integer or zero" }
    val b1 = BigDecimal(v1.toString())
    val b2 = BigDecimal(v2.toString())
    return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).toDouble()
}